﻿Imports System.Text.RegularExpressions

Public Class Antena3
    Inherits WebSite

    Private Enum A3Sections
        Home = 100

        SeriesHome = 200
        SeriesSeasons = 201
        SeriesEpisodes = 202

        NoticiasHome = 300
        NoticiasYears = 301
        NoticiasMonths = 302
        NoticiasEpisodes = 303

        ProgramasHome = 400
        ProgramasYears = 401
        ProgramasMonths = 402
        ProgramasEpisodes = 403

        InfantilesHome = 500
        InfantilesSeasons = 501
        InfantilesEpisodes = 502

        MoviesHome = 600
    End Enum

    Public Sub New()
        MyBase.BaseURL = "http://www.antena3.com"
        MyBase.Name = "Antena 3"
        MyBase.Site = WebSites.Antena3
    End Sub

    Public Overrides Function GetCategories() As System.Collections.Generic.List(Of Category)
        Dim cats As New List(Of Category)
        Dim webData As String = SiteUtilBase.GetWebData(TVALACartaUtil.CombinePath(MyBase.BaseURL, "/videos/"))
        Dim m = Regex.Match(webData, "<li(?:\sclass=""active"")?><a\stitle=""(?<description>[^""]*)""\shref=""(?<url>[^""]*)""(?:\srel=""index"")?>(?<title>[^<]*)</a></li>")
        While m.Success
            Dim newCat = New RssLink()
            newCat.Name = m.Groups("title").Value
            newCat.Url = TVALACartaUtil.CombinePath(MyBase.BaseURL, m.Groups("url").Value)
            newCat.Description = m.Groups("description").Value
            newCat.HasSubCategories = (newCat.Name <> "HOME")

            Select Case newCat.Name
                Case "HOME" : newCat.Other = A3Sections.Home
                Case "SERIES" : newCat.Other = A3Sections.SeriesHome
                Case "NOTICIAS" : newCat.Other = A3Sections.NoticiasHome
                Case "PROGRAMAS" : newCat.Other = A3Sections.ProgramasHome
                Case "INFANTIL" : newCat.Other = A3Sections.InfantilesHome
                Case "TV MOVIES" : newCat.Other = A3Sections.MoviesHome
            End Select

            cats.Add(newCat)

            m = m.NextMatch()
        End While

        Return cats
    End Function

    Public Overrides Function GetSubCategories(parentCategory As Category) As System.Collections.Generic.List(Of Category)
        Dim cats As New List(Of Category)
        Dim link = CType(parentCategory, RssLink)
        Dim webData As String = SiteUtilBase.GetWebData(link.Url)

        Dim regExp As String = ""
        Dim currentSection As A3Sections = CType(link.Other, A3Sections)
        Dim nextSection As A3Sections = currentSection
        Dim hasSubCats As Boolean = False

        ' TODO: Try to improve the RegExs to make them more generic and, hopefully, use just a couple of them to find all the videos, instead of using one for each section.
        Select Case currentSection
            Case A3Sections.Home
                regExp = "<img\stitle=""(?<title>[^""]*)""\s*src=""(?<thumb>[^""]*)""\s*alt=""(?<description>[^""]*)""\s*href=""(?<url>[^""]*)""\s*/>"

            Case A3Sections.SeriesHome
                regExp = "<img\stitle=""(?<description>[^""]*)""\shref=""(?<url>[^""]*)""\s*src=""(?<thumb>[^""]*)""\s*alt=""(?<title>[^""]*)""\s*/>"
                nextSection = A3Sections.SeriesSeasons
                hasSubCats = True
            Case A3Sections.SeriesSeasons
                regExp = "<a\s*title=""(?<description>[^""]*)""\s*href=""(?<url>[^""]*)""\s>\s*(?<title>[^\s]*)\s*</a>"
                nextSection = A3Sections.SeriesEpisodes

            Case A3Sections.NoticiasHome
                regExp = "<img\stitle=""(?<description>[^""]*)""\s*src=""(?<thumb>[^""]*)""\s*alt=""(?<title>[^""]*)""\s*href=""(?<url>[^""]*)""\s*/>"
                nextSection = A3Sections.NoticiasYears
                hasSubCats = True
            Case A3Sections.NoticiasYears
                regExp = "<li(?:\s*class=""active"")?\s><a\stitle=""(?<description>[^""]*)""\shref=""(?<url>[^""]*)""\s>(?<title>[^<]*)</a></li>"
                nextSection = A3Sections.NoticiasMonths
                hasSubCats = True
            Case A3Sections.NoticiasMonths
                regExp = "<li\s*(?:class=""active""\s*)?>\s*<a\stitle=""(?<description>[^""]*)""\s*href=""(?<url>[^""]*)""\stitle=""(?<description>[^""]*)"">(?<title>[^<]*)</a></li>"
                nextSection = A3Sections.NoticiasEpisodes

            Case A3Sections.ProgramasHome
                regExp = "<img\stitle=""(?<description>[^""]*)""\s*src=""(?<thumb>[^""]*)""\s*alt=""(?<title>[^""]*)""\s*href=""(?<url>[^""]*)""\s*/>"
                nextSection = A3Sections.ProgramasYears
                hasSubCats = True
            Case A3Sections.ProgramasYears
                regExp = "<li(?:\s*class=""active"")?\s><a\stitle=""(?<description>[^""]*)""\shref=""(?<url>[^""]*)""\s>(?<title>[^<]*)</a></li>"
                nextSection = A3Sections.ProgramasMonths
                hasSubCats = True
            Case A3Sections.ProgramasMonths
                regExp = "<li\s*(?:class=""active""\s*)?>\s*<a\stitle=""(?<description>[^""]*)""\s*href=""(?<url>[^""]*)""\stitle=""(?<description>[^""]*)"">(?<title>[^<]*)</a></li>"
                nextSection = A3Sections.ProgramasEpisodes

            Case A3Sections.InfantilesHome
                regExp = "<img\stitle=""(?<description>[^""]*)""\shref=""(?<url>[^""]*)""\s*src=""(?<thumb>[^""]*)""\s*alt=""(?<title>[^""]*)""\s*/>"
                nextSection = A3Sections.InfantilesSeasons
                hasSubCats = True
            Case A3Sections.InfantilesSeasons
                regExp = "<a\s*title=""(?<description>[^""]*)""\s*href=""(?<url>[^""]*)""\s>\s*(?<title>[^\s]*)\s*</a>"
                nextSection = A3Sections.InfantilesEpisodes

            Case A3Sections.MoviesHome
                regExp = "<img\stitle=""(?<description>[^""]*)""\s*src=""(?<thumb>[^""]*)""\s*alt=""(?<title>[^""]*)""\s*href=""(?<url>[^""]*)""\s*/>"
        End Select

        Dim m = Regex.Match(webData, regExp)
        While m.Success
            Dim newCat = New RssLink()
            newCat.Name = m.Groups("title").Value
            newCat.Url = TVALACartaUtil.CombinePath(MyBase.BaseURL, m.Groups("url").Value)
            newCat.Thumb = TVALACartaUtil.CombinePath(MyBase.BaseURL, m.Groups("thumb").Value)
            newCat.Description = m.Groups("description").Value
            newCat.HasSubCategories = hasSubCats
            newCat.Other = nextSection

            cats.Add(newCat)

            m = m.NextMatch()
        End While

        Return cats
    End Function

    Public Overrides Function GetVideos(category As Category) As List(Of VideoInfo)
        Dim videos As New List(Of VideoInfo)
        Dim link = CType(category, RssLink)
        Dim webData As String = SiteUtilBase.GetWebData(link.Url)

        Dim regExp As String
        Dim currentSection As A3Sections = CType(link.Other, A3Sections)

        Select Case currentSection
            Case A3Sections.Home
                regExp = "<img\stitle=""(?<description>[^""]*)""\s*src=""(?<thumb>[^""]*)""\s*alt=""(?<title>[^""]*)""\s*href=""(?<url>[^""]*)""\s*/>"
            Case Else
                regExp = "<img\stitle=""[^""]*""\s*src=""(?<thumb>[^""]*)""\s*alt=""[^""]*""\s*href=""(?<url>[^""]*)""\s*/>\s*<em\sclass=""play_video""><img\stitle=""ver\svideo""\ssrc=""/static/modosalon/images/button_play_s1\.png""\salt=""ver\svideo""/></em>\s*<strong>(?<description>[^<]*)</strong>\s*<h2><p>(?<title>[^<]*)</p></h2>"
        End Select

        Dim m = Regex.Match(webData, regExp)
        While m.Success
            Dim newVideo = New VideoInfo()
            newVideo.Title = m.Groups("title").Value
            newVideo.VideoUrl = TVALaCartaUtil.CombinePath(MyBase.BaseURL, m.Groups("url").Value)
            newVideo.ImageUrl = TVALACartaUtil.CombinePath(MyBase.BaseURL, m.Groups("thumb").Value)
            newVideo.Description = m.Groups("description").Value

            videos.Add(newVideo)

            m = m.NextMatch()
        End While

        Return videos
    End Function

    Public Overrides Function GetVideoURL(video As VideoInfo) As String
        Dim webData = SiteUtilBase.GetWebData(video.VideoUrl)
        Dim m = Regex.Match(webData, "player_capitulo.xml='(?<url>[^'']*)'")

        If m.Success Then
            Dim xmlData As XDocument = XDocument.Parse(SiteUtilBase.GetWebData(TVALACartaUtil.CombinePath(MyBase.BaseURL, m.Groups("url").Value)))
            Dim fileName As String = ""
            For Each item In xmlData...<archivoMultimedia>
                If item.Value.EndsWith("mp4") Then
                    fileName = xmlData...<urlVideoMp4>.Value + item.Value
                    Exit For
                End If

                If item.Value.EndsWith("f4v") Then
                    fileName = xmlData...<urlVideoFlv>.Value + item.Value
                    Exit For
                End If
            Next
            Return fileName
        Else
            Return ""
        End If
    End Function
End Class
